import xmlrpclib
import time # needed for sleep

############################################################################
#
# In case login and password are specified in fibercomb.setup,
# we also that here
#

# format for server-url: http://{LOGIN}:{PASSWORD}@{HOST}:{PORT}/RPC2
# '{LOGIN}:{PASSSWORD}@' can be omitted if password protection is disabled
#
server_url = 'http://comb:system@localhost:8123/RPC2';


# Create an object to represent our server.
server = xmlrpclib.ServerProxy(server_url);


############################################################################
#
# First, we want to query the software version of the server:
#
print "Talking to Server:", server.hello()

############################################################################
#
# XML-RPC introspection allows us to learn about method names
# and how to call them (may be incomplete according to XML-RPC specs)
#
print "Supported Methods:", server.system.listMethods()

print "\nSignature of method 'data.getInfo':"
print server.system.methodSignature('data.getInfo');

print "\nHelp on method 'data.getInfo':"
print server.system.methodHelp('data.getInfo');

print "\nSignature of method 'data.query':"
print server.system.methodSignature('data.query');

print "\nHelp on method 'data.query':"
print server.system.methodHelp('data.query');

############################################################################
#
# Now to the comb specific part, first we query information on the
# structure of the data:
#
print "\nAvailable Data:"

dataInfo= server.data.getInfo()
dataNames= dataInfo.keys()
dataNames.sort()

for name in dataNames:
    print " ",name,"=>", dataInfo[name]

result = server.comb.reprate.synth.getFreq()
print "Synthesizer is at:", result

#server.comb.reprate.synth.setFreq(result+0.001)

print "Synthesizer is at:", server.comb.reprate.synth.getFreq()

print "Reprate harmonic: ", server.param.query("beatcalc.reprate.harmonic");


############################################################################
#
# 
#
print "\nDumping some data in cache:"

# only get the 5 most recent data sets:
data= server.data.query(-5.000)
if (type(data)!=type('str')):
    timestamps= data.keys()
    timestamps.sort()
    
    for ts in timestamps:
        print " ",ts,"=>"
        names= data[ts].keys()
        names.sort()
        for value in names:
            print "          ", value,"=",data[ts][value]
        
timestamp= server.data.last_timestamp()
print "\nTimestamp of most recent data is",timestamp,"\n"

print "\nStarting endless data readout:"

############################################################################
#
# define some channels we want to read out
# invalid names are ignored by the server but may lead to troubles later...
#
myDataChannels=['timestamp.abs',
                'counter0.freq',
                'counter1.freq',
                'reprate.freq',
                'reprate.freqref']

# now read the data in an endless loop:
while 1:
    print "new query, last timetamp was",timestamp

    #read only the specified channels
    #data= server.data.query(timestamp+0.1,myDataChannels)

    #or read everything and only print some
    data= server.data.query(timestamp+0.1)

    # an empty reply may not be of type 'dict', so check this:
    if (type(data).__name__=='dict'):

        # more than one data set may be present
        timestamps= data.keys()
        timestamps.sort()

        for ts in timestamps:
            # remember timestamp so we can query more recent data later
            # note that ts is a string and we need timestamp as double
            timestamp= float(ts)

            # now print the data
            print "  %.3f =>" % timestamp
            for value in myDataChannels:
                print "          ", value,"=",data[ts][value]
                
    data=''
    time.sleep(0.1)


